const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isManage: false, //管理按钮状态，true为可进行删除操作
        list: [], //购物车列表
        checked: false, //是否全选
        picPath: app.globalData.path,
        checkedArr: [], //勾选的商品
        number: 0, //勾选商品件数
        total: 0, //勾选商品总金额

    },

    checkAllHandle() {
        var checked = !this.data.checked, list = this.data.list;
        list.forEach(item => {
            item.checked = checked
        })
        this.setData({
            checked,
            list
        })
        this.getTotalNum()
    },


    // 改变商品数量
    changeNum(e) {
        var type = e.currentTarget.dataset.type;
        if (e.currentTarget.dataset.flag == 'add') {
            e.currentTarget.dataset.item.productNum++
        } else {
            e.currentTarget.dataset.item.productNum--
        }
        var str = `list[${e.currentTarget.dataset.index}]`
        this.setData({
            [str]: e.currentTarget.dataset.item,
        })
        this.getTotalNum()
    },

    getTotalNum() {
        var num = 0
        var total = 0, list = this.data.list;
        list.forEach(item => {
            if (item.checked) {
                num += item.productNum
                total += item.productNum * item.formatPrice
            }
        })
        this.setData({
            number: num,
            total: total
        })
    },


    manageClick: function () { //管理按钮状态切换
        this.setData({
            isManage: !this.data.isManage
        })
    },

    // 勾选单个商品
    onChange(e) {
        var list = this.data.list, checked = this.data.checked;
        list[e.currentTarget.dataset.index].checked = !list[e.currentTarget.dataset.index].checked
        checked = list.every(item => {
            return item.checked
        })
        this.setData({
            list, checked
        })
        this.getTotalNum()
    },

    // 商品详情
    openDetail(e) {
        console.log(e);
        wx.navigateTo({
            url: '/packageProduct/pages/productDetail/productDetail?id=' + e.currentTarget.dataset.proid,
        })
    },

    // 删除商品
    delFunc() {
        var list = this.data.list, ids = '', that = this;
        if (!list.some(item => {
            return item.checked
        })) {
            wx.showToast({
                title: '请勾选商品',
                icon: 'none'
            })
            return
        }

        list.forEach(item => {
            if (item.checked) {
                ids += item.cartId + ','
            }
        })

        wx.showModal({
            title: '提示',
            content: '是否删除勾选的商品',
            success(res) {
                if (res.confirm) {
                    that.delPro(ids, true)
                } else if (res.cancel) { }
            }
        })
    },

    // 删除商品请求
    delPro(ids) {
        console.log(ids);

        var that = this, url = app.globalData.url + 'main/delCart',
            data = {
                ids,
            };
        app.wxRequest(url, data, res => {
            if (res.flag) {
                that.getData()
                wx.showToast({
                    title: '删除成功',
                    icon: 'none'
                })
            } else {
                wx.showToast({
                    title: '删除失败，请重试',
                    icon: 'none'
                })
            }
        })
    },

    // 跳转到商城页
    openMain() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },

    // 结算 
    openOrder(e) {
        var list = this.data.list, proList = [];

        if (!list.some(item => {
            return item.checked
        })) {
            wx.showToast({
                title: '请勾选商品',
                icon: 'none'
            })
            return
        }

        proList = list.filter(item => {
            return item.checked
        })

        console.log(proList);

        wx.navigateTo({
            url: '/packageProduct/pages/submitOrder/submitOrder?product=' + JSON.stringify(proList)
        })

    },


    // 获取数据
    getData() {
        var that = this, url = app.globalData.url + 'main/listUserCart',
            data = {
                userId: app.globalData.userId,
            };

        wx.showLoading({
            title: '',

        })
        app.wxRequest(url, data, res => {
            wx.hideLoading()
            console.log(res);
            var datas = res.data;
            datas.forEach(item => {
                item.checked = false
            })

            that.setData({
                list: datas,
            })

        }, err => { });

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getData()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})